import {
  getToken,
  getUserInfo,
  removeToken,
  removeUserInfo,
  setToken,
  setUserInfo,
} from "@/utils/storage";

const user = {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: getUserInfo(),
  },
  mutations: {
    updateToken(state, newToken) {
      setToken(newToken);
      state.token = newToken;
      // console.log(newToken);
    },
    delToken(state) {
      removeToken();
      state.token = null;
    },
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo;
      // console.log(userInfo);
      setUserInfo(userInfo);
    },
    delUserInfo(state) {
      state.UserInfo = {};
      removeUserInfo();
    },
  },
  actions: {
    logoutAction(ctx) {
      ctx.commit("delToken");
      ctx.commit("delUserInfo");
    },
  },
};

export default user;
